﻿using Credera.Theta.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Credera.Theta.Modules.Environments.Commands
{
    [Command("ListEnvironmentDetails")]
    [Synopsis("Lists the details of the given environment")]
    class ListEnvironmentDetailsCommand : InputCommand
    {
        [Parameter(ordinalPosition: 1, autoCompleteValuesFunction: "GetEnvironmentNamesAutoComplete", useAutoCompleteForValidation: true)]
        [Synopsis("The name of the environment to list details for")]
        public string Environment { get; set; }

        public override void Invoke(CommandInvocationContext context)
        {
            var env = EnvironmentLogic.Instance.EnvironmentsCollection.Environments
                .FirstOrDefault(e => e.Name.Equals(this.Environment, StringComparison.OrdinalIgnoreCase));

            base.Out.Standard.WriteLine();
            base.Out.Standard.WriteLine(string.Format("     Environment:    {0}", env.Name));
            base.Out.Standard.WriteLine(string.Format("     Is Loaded:      {0}", EnvironmentLogic.Instance.LoadedEnvironments.Contains(env)));
            base.Out.Object.Table(TableRecords.CreateEnvironmentModuleTableRecordList(env.Modules.ToArray()));
        }

        public void GetEnvironmentNamesAutoComplete(AutoCompleteValuesFunctionContext context)
        {
            context.Values.AddRange(EnvironmentLogic.Instance.EnvironmentsCollection.Environments.Select(e => e.Name));
        }
    }
}
